package jlts.front.ui;

import jlts.front.core.UiController;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @ClassName LoginFrame
 * @Description TODO
 * @Author Zxeao
 * @Date 2021/12/21 上午11:12
 * @Version V1.0
 */
public class LoginFrame extends JFrame {

    private JPanel contentPanel = new JPanel();

    private JPanel northPanel = new JPanel();
    private JPanel centerPanel = new JPanel();
    private JPanel southPanel = new JPanel();

    private JLabel titleLabel = new JLabel();
    private JLabel unameLabel = new JLabel();
    private JLabel upwdLabel = new JLabel();
    private JLabel msgLabel = new JLabel();

    private JTextField unameField = new JTextField();
    private JPasswordField upwdField = new JPasswordField();

    private JButton loginBtn = new JButton();
    private JButton cancelBtn = new JButton();

    /**界面控制器 需要注入此对象，因为调用login的时候用*/
    private UiController uiController;

    /**获取用户名输入框的值*/
    public String getUnameText(){
        return this.unameField.getText();
    }
    /**获取密码输入框的值*/
    public String getUpwdText(){
        char[] chs=this.upwdField.getPassword();
        return new String(chs);
    }
    /**更新错误信息*/
    public void updateErrMsg(String msg){
        this.msgLabel.setText(msg);
    }
    public void setUiController(UiController uiController) {
        this.uiController = uiController;
    }

    public LoginFrame() {
        this.setSize(280,200);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setLocationRelativeTo(null);
        initContentPane();
        this.add(contentPanel);
        this.setResizable(false);
        this.setVisible(true);
    }

    private void initContentPane() {
        //设置面板布局
        contentPanel.setLayout(new BorderLayout());
        //设置边界
        contentPanel.setBorder(new EmptyBorder(8,8,8,8));
        //初始化组件
        initNorthPane();
        initCenterPane();
        initSouthPane();
        //添加组件
        contentPanel.add(northPanel,BorderLayout.NORTH);
        contentPanel.add(centerPanel,BorderLayout.CENTER);
        contentPanel.add(southPanel,BorderLayout.SOUTH);
    }

    public  void initNorthPane() {
        northPanel.setLayout(new BorderLayout());
        northPanel.setBorder(new EmptyBorder(0,0,0,0));
        titleLabel.setText("登录考试系统");
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        northPanel.add(titleLabel,BorderLayout.CENTER);
    }

    public  void initCenterPane() {
        centerPanel.setLayout(new GridLayout(3,2));
        centerPanel.setBorder(new EmptyBorder(6,6,6,6));
        initUnameAndPwd();
        centerPanel.add(unameLabel);
        centerPanel.add(unameField);
        centerPanel.add(upwdLabel);
        centerPanel.add(upwdField);
        centerPanel.add(msgLabel);
    }

    public  void initSouthPane() {
        southPanel.setLayout(new FlowLayout());
        southPanel.setBorder(new EmptyBorder(6,6,6,6));
        initBtn();
        southPanel.add(loginBtn);
        southPanel.add(cancelBtn);
    }

    public void initUnameAndPwd(){
        unameLabel.setText("用户名：");
        upwdLabel.setText("密码：");
        unameField.setColumns(10);
        upwdField.setColumns(10);
        //提示信息
        msgLabel.setText("");
        msgLabel.setForeground(Color.RED);
    }

    public void initBtn(){
        loginBtn.setText("登录");
        cancelBtn.setText("取消");

        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //调用控制器的login方法
                uiController.login();
            }
        });

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result = JOptionPane.showConfirmDialog(null,"你确定退出吗？","提示",JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION){
                    System.exit(0);
                }
            }
        });


    }
}
